﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClientAppraise.ViewModels.Validator
{
    public sealed class ZxRangeAttribute : ValidationAttribute
    {
        double? _min;
        double? _max;

        public ZxRangeAttribute(double? min, double? max)
        {
            this._min = min;
            this._max = max;
        }

        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if (value == default) return ValidationResult.Success;

            var strValue = value.ToString();

            if (string.IsNullOrEmpty(strValue)) return ValidationResult.Success;


            if (!double.TryParse(value.ToString(), out var theValue))
            {
                return new($"只能位于{_min},{_max}之间的数字");
            }

            if (theValue < _min || theValue > _max)
            {
                return new($"只能位于{_min},{_max}之间的数字");
            }

            return ValidationResult.Success;
        }
    }
}
